var mongoose = require('../db.js'),
    Schema = mongoose.Schema;

var ProductSchema = new Schema({
    title: { type: String, default: '', trim: true },
    tags: { type: Array},
    image: {
        cdnUri: String,
        files: Array,
        descript: Array
    },
    price: { type: Number },
    createdAt: { type: Date, default: Date.now }
});


/**
 * Validations
 */
ProductSchema.path('title').validate(function (title) {
    return title.length > 0
}, 'Product title cannot be blank');


/**
 * Methods
 */

ProductSchema.methods = {
    /**
     * Save article and upload image
     *
     * @param {Object} product
     * @param {Function} cb
     * @api private
     */

    uploadAndSave: function (product, cb) {
        return this.save(product, cb)
    },

    findOneAndUpdate: function (condition, option, cb) {
        return this.findOneAndUpdate(condition, option, cb)
    },
    findByIdAndUpdate: function (id, condition, cb) {
        return this.findByIdAndUpdate(id, condition, cb)
    },
    findOneAndRemove: function (condition, option, cb) {
        return this.findOneAndRemove(condition, option, cb)
    },
    findByIdAndRemove: function (id, cb) {
        return this.findByIdAndRemove(id, cb)
    }
};

ProductSchema.static = {
    load: function () {
    }
};

module.exports = mongoose.model('Product', ProductSchema)
